package com.gksc.video.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.gksc.video.data.DeviceInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @ClassName: Tool
 * @Description: 工具类
 * @author: Liao.yx
 * @date: 2021年3月8日14:23:26
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 */
@Slf4j
public class Tool {
    private static long workId = 1;
    private static long dataCenterId = 1;

    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     * @throws IOException
     */
    public static String getRealIpAddress(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    public static boolean checkIpAddress(String ip) {
        return ip.matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
    }

    /**
     * 获取唯一ID值
     *
     * @return
     */
    public static String getNextId() {
        Snowflake snowflake = IdUtil.getSnowflake(workId, dataCenterId);
        BigDecimal nextId = new BigDecimal(snowflake.nextIdStr());
        return nextId.toString();
    }

    /**
     * 检查设备信息
     *
     * @param
     * @return
     */
    public static boolean checkDeviceInfo(String ip, String port, String userName, String password) {
        if (StringUtils.isEmpty(ip)) {
            return false;
        }
        if (StringUtils.isEmpty(port)) {
            return false;
        }
        if (StringUtils.isEmpty(userName)) {
            return false;
        }
        return !StringUtils.isEmpty(password);
    }

    /**
     * 从rtsp地址中获取设备IP地址
     *
     * @param url rtsp地址
     * @return
     */
    public static String getDeviceIpByRtsp(String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        int begin = url.indexOf("@");
        String sub = url.substring(begin, begin + 17);
        String[] arr = sub.split(":");
        return arr[0];
    }

    /**
     * 检查按设备推流方式是否重复
     *
     * @param device  请求信息
     * @param cache   缓存信息
     * @param history 是否为回放
     * @return
     */
    public static boolean checkDevicePush(DeviceInfo device, DeviceInfo cache, boolean history) {
        if (!cache.getDevIp().equals(device.getDevIp())) {
            return false;
        }
        if (!cache.getDevPort().equals(device.getDevPort())) {
            return false;
        }
        if (!cache.getUsername().equals(device.getUsername())) {
            return false;
        }
        if (!cache.getPassword().equals(device.getPassword())) {
            return false;
        }
        if (!cache.getDevFirm().equals(device.getDevFirm())) {
            return false;
        }
        if (!cache.getOpenTime().equals(device.getOpenTime())) {
            return false;
        }
        if (history) {
            if (!cache.getStartTime().equals(device.getStartTime())) {
                return false;
            }
            return cache.getEndTime().equals(device.getEndTime());
        }
        return true;
    }
}
